<template>
  <el-dialog
      v-model="ledgerAddWinDialogVisible"
      title="新增台账"
      width="60%"
      @close="handleClose"
      draggable
      center
  >
    <template #default>
      <el-form :model="newLedger">
        <el-form-item label="设备编码">
          <el-input v-model="newLedger.ledgerEncode"/>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="newLedger.ledgerName"/>
        </el-form-item>
        <el-form-item label="品牌">
          <el-input v-model="newLedger.ledgerBrand"/>
        </el-form-item>
        <el-form-item label="设备分类">
          <el-select v-model="newLedger.ledgerType">
            <el-option label="家用" :value="0"/>
            <el-option label="军用" :value="1"/>
            <el-option label="企业" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号">
          <el-input type="textarea" v-model="newLedger.ledgerModel"/>
        </el-form-item>
        <el-form-item label="所属车间">
          <el-select v-model="newLedger.ledgerWorkshop">
            <el-option label="家用车间" :value="0"/>
            <el-option label="军用车间" :value="1"/>
            <el-option label="企业车间" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="newLedger.ledgerStatus">
            <el-radio value="1" size="large">开机</el-radio>
            <el-radio value="2" size="large">停机</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="newLedger.ledgerRemark"/>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="addLedgerSubmit">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {ref, watch} from "vue";
import ledgerRequest from "@/api/ledger.js";
// 保存新增项目表单的数据
let newLedger = ref({});
const props = defineProps(['ledgerFlag']);
const emits = defineEmits(['cancelAdd','addComplete'])
let ledgerAddWinDialogVisible = ref(false);

/**
 * 关闭窗体前的回调函数
 */
function handleClose(){
  newLedger = ref({});
  emits("cancelAdd");
}
function cancel(){
  newLedger = ref({});
  emits("cancelAdd");
}
/**
 * 新增台账的提交按钮事件
 */
function addLedgerSubmit() {
  ledgerRequest.insert(newLedger.value, res => {
    emits("addComplete");
    ledgerAddWinDialogVisible.value = false;
  })
}

// 属性侦听
watch(() => props.ledgerFlag, () => {
  ledgerAddWinDialogVisible.value = props.ledgerFlag === 1;
});
</script>
<style scoped>

</style>